import JsonP from 'jsonp';
import axios from 'axios';
import {message} from 'antd';

export default class Axios {

    // 封装jsonp 跨域调用天气api
    static jsonp(option){
        return new Promise(((resolve, reject) => {
            JsonP(option.url,{
                param: 'callback'
            },(err,response) => {
                if(response.status === 'success'){
                    resolve(response);
                }else{
                    reject(response.message);
                }
            })
        }))
    }

    // 封装axios
    static Axios(options) {
        let baseUrl = "https://easy-mock.com/mock/5b85f226b6eb682fc7f9ef9d/bicycleApi/";
        return new Promise((resolve,reject) => {
            axios({
                url:baseUrl + options.url,
                method:options.method,
                timeout:25000, // 超时报错
                params: (options.params ? options.params : ''), // GET
                data:options.data // POST
            }).then(response => {
                if(response.status === 200){
                    let res = response.data;
                    if(res.code === 0){
                        resolve(res.data)
                    }else{
                        message.error(res.message)
                    }
                }
            }).catch(error => {
                reject(error)
            })
        })
    }
}
// fixme 做成全局loading
// let loading;
// if(options.isShowLoading !== false){
//     loading = document.getElementById('ajaxLoading');
//     loading.style.display = "block";
// }

// 成功之后引用
// if(options.isShowLoading !== false){
//     loading = document.getElementById('ajaxLoading');
//     loading.style.display = "none";
// }